package com.edu.lawdoc.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ModifyingFields {
    Map<String, Field> map = new HashMap<>();

    public ModifyingFields() {}

    public ModifyingFields(Map<String, Object> paraMap) {
        paraMap.forEach((k, v) -> this.put(k, v));
    }

    public ModifyingFields(Map<String, Object> paraMap, Map<String, Function> resolves, String[] exceptKeys) {
        paraMap.forEach((k, v) -> {
            if(com.edu.lawdoc.api.utils.ArrayKit.contains(exceptKeys, k)) {
                return;
            }

            Function fun = resolves.get(k);
            if(fun == null) {
                /**
                 * resolve没有值的时候，直接赋值给ModifyingFields的map
                 */
                this.put(k, v);
                return;
            }

            /**
             * resolve有值的时候，说明需要进行对应的处理了
             */
            this.put(k, fun.apply(v));
        });
    }

    public void put(String key, Object value) {
        Field oField = new Field();
        oField.setKey(key);
        oField.setValue(value);
        map.put(key, oField);
    }

    public Object get(String key) {
        Field field = map.get(key);
        if(field == null) {
            return null;
        }

        return field.getValue();
    }

    /**
     * 使用了索引的就会排在最前面
     * @return
     */
    public List<Field> getPara() {
        return map.values().stream().collect(Collectors.toList());
    }

    /**
     * 判断是否包含某个键
     * @param key
     * @return
     */
    public boolean containsKey(String key) {
        return map.containsKey(key);
    }
}
